<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools :title="`共${total}条`">
        <el-button type="danger" size="small" @click="exportExcel">普通Excel导出</el-button>
        <el-button type="info" size="small" @click="exportExcel2">复杂表头Excel导出</el-button>
        <el-button type="success" size="small" @click="$router.push('/import?type=user')">Excel导入</el-button>
        <el-button type="primary" size="small" @click="$refs.form.showDialog=true">新增员工</el-button>
      </PageTools>
      <!-- 表格 -->
      <el-table :data="list" border stripe>
        <el-table-column label="序号" type="index" />
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="头像" prop="staffPhoto" />
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <el-table-column label="聘用形式" prop="formOfEmployment">
          <template slot-scope="scope">
            <span v-if="scope.row.formOfEmployment*1 === 1">正式员工</span>
            <span v-if="scope.row.formOfEmployment*1 === 2">临时工</span>
          </template>
        </el-table-column>
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="入职时间" prop="timeOfEntry" />
        <el-table-column label="操作" width="300">
          <template slot-scope="{row}">
            <el-button type="text" @click="$router.push('/employees/detail/'+row.id)">查看</el-button>
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text">角色</el-button>
            <el-button type="text" @click="delHandler(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="queryData.page"
        :page-sizes="[10, 12, 15, 20]"
        :page-size="queryData.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <AddDept ref="form" @update="initData" />
  </div>
</template>

<script>
import { getUserListData, delUsertData } from '@/api/employees'
import PageTools from '@/components/PageTools'
import AddDept from './components/add-dept.vue'
import { formatDate } from '@/filters/index'
export default {
  components: {
    PageTools,
    AddDept
  },
  data() {
    return {
      queryData: {
        page: 1,
        size: 10
      },
      total: 0,
      list: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      // 发去请求
      const { rows, total } = await getUserListData(this.queryData)
      this.list = rows
      this.total = total
    },
    // 每页数量切换事件
    handleSizeChange(val) {
      this.queryData.size = val
      this.initData()
    },
    // 页码切换事件
    handleCurrentChange(val) {
      this.queryData.page = val
      this.initData()
    },
    // 删除
    async delHandler(id) {
      await this.$confirm('确认删除？')
      await delUsertData(id)
      this.initData()
    },
    // 普通导出
    exportExcel() {
      // import('路径') 动态加载模块
      import('@/vendor/Export2Excel').then(async excel => {
        // excel就是导入的整个模块
        // export_json_to_excel将json数据导入到excel中
        const { headerData, bodyData } = await this.getData()
        // 3、调用插件的导出方法
        excel.export_json_to_excel({
          header: headerData, // 表头
          data: bodyData,
          filename: '员工信息'
        })
      })
    },
    // 复杂导出
    exportExcel2() {
      // import('路径') 动态加载模块
      import('@/vendor/Export2Excel').then(async excel => {
        // excel就是导入的整个模块
        // export_json_to_excel将json数据导入到excel中
        const { headerData, bodyData } = await this.getData()
        // 3、调用插件的导出方法
        excel.export_json_to_excel({
          multiHeader: [['姓名', '基本信息', '', '', '', '', '部门']],
          merges: ['A1:A2', 'B1:F1', 'G1:G2'],
          header: headerData, // 表头
          data: bodyData,
          filename: '复杂员工信息'
        })
      })
    },
    // 请求数据和整理数据
    async getData() {
      // 1、请求要导出的数据
      const { rows } = await getUserListData({ page: 1, size: 111111 })
      // 2、整理数据
      const headers = {
        '手机号': 'mobile',
        '姓名': 'username',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      // headers里所有的中文
      // headerData[手机号，姓名，入职日期。。。]
      const headerData = Object.keys(headers)
      console.log(headerData)
      const bodyData = rows.map(item => {
        // item是每一个数据对象
        const res = headerData.map(key => {
          // key是每一个中文  headers[key] 是每一个中文对应的英文
          if (['timeOfEntry', 'correctionTime'].includes(headers[key])) {
            return formatDate(item[headers[key]])
          } else {
            return item[headers[key]]
          }
        })
        return res
      })
      return { headerData, bodyData }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
